package com.syezon.wifikey.ad;

import android.text.TextUtils;

import com.blankj.utilcode.util.SPUtils;
import com.hodanet.appadvplatclient.ad.AdInfo;
import com.syezon.wifikey.utils.DateUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class OptVideoAdConfig {

    public static final String VIDEO_AD_SOURCE_DEFAULT = "default";
    public static final String VIDEO_AD_SOURCE_GDT = "GDT";
    public static final String VIDEO_AD_SOURCE_TT = "CSJ";
    public static final String VIDEO_AD_SOURCE_KY = "KY";
    public static final String DEFAULT_VIDEO_AD_ORDER = "GDT,CSJ";
    private static final String CONFIG_NAME = "video_order_config";
    private static final String VIDEO_SOURCE = "SPLASH_AD_SOURCE";
    private static final String LAST_VIDEO_AD_CODE = "last_splash_ad_code";
    private static final String VIDEO_AD_ORDER = "video_ad_order";
    public static final String LAST_VIDEO_SHOW_DAY = "last_video_show_day";


    /**
     * @return 获取开屏广告来源
     */
    public static String getVideoAdSource() {
        try {
            int checkCount = 0;
            String lastVideoAdCode = getLastVideoAdCode();
            String videoAdOrder = getVideoAdOrder();
            if (!TextUtils.isEmpty(videoAdOrder)) {
                String[] videoOrderArray = DEFAULT_VIDEO_AD_ORDER.split(",");
                if (videoOrderArray.length > 0) {
                    List<String> videoOrderList = new ArrayList<>(Arrays.asList(videoOrderArray));
                    videoOrderList.remove(lastVideoAdCode);
                    int lastVideoOrderIndex = videoOrderList.indexOf(lastVideoAdCode);
                    int splashAdCodeIndex = 0;
                    if (lastVideoOrderIndex != -1) {
                        splashAdCodeIndex = (lastVideoOrderIndex + 1) % videoOrderList.size();
                    }
                    while (checkCount < videoOrderList.size()) {
                        String videoAdCode = videoOrderList.get(splashAdCodeIndex % videoOrderList.size());
                        if ((VIDEO_AD_SOURCE_GDT.equals(videoAdCode) && AdConfig.canShowGDTAd())
                                || (VIDEO_AD_SOURCE_TT.equals(videoAdCode) && AdConfig.canShowCSJAd())
                        ) {
                            return videoAdCode;
                        } else {
                            splashAdCodeIndex++;
                            checkCount++;
                        }
                    }
                }
            }
        } catch (Exception e) {

        }
        return VIDEO_AD_SOURCE_DEFAULT;
    }

    public static List<String> getVideoAdOrderList() {
        try {
            String videoAdOrder = getVideoAdOrder();
            if (!TextUtils.isEmpty(videoAdOrder)) {
                String[] videoOrderArray = videoAdOrder.split(",");
                if (videoOrderArray.length > 0) {
                    List<String> videoOrderList = new ArrayList<>(Arrays.asList(videoOrderArray));
                    return videoOrderList;
                }
            }
        } catch (Exception e) {

        }
        return Arrays.asList(VIDEO_AD_SOURCE_GDT, VIDEO_AD_SOURCE_TT, VIDEO_AD_SOURCE_KY);
    }

    public static String getNextVideoAdCode(String currentVideoSourceCode, List<String> videoAdOrderList, int loadCount) {
        if (!TextUtils.isEmpty(currentVideoSourceCode) && videoAdOrderList != null && !videoAdOrderList.isEmpty()) {
            if (videoAdOrderList.size() == 1) {
                if (loadCount == 1) {
                    if (VIDEO_AD_SOURCE_GDT.equals(currentVideoSourceCode) && AdConfig.canShowCSJAd()) {
                        return VIDEO_AD_SOURCE_TT;
                    }
                    if (VIDEO_AD_SOURCE_TT.equals(currentVideoSourceCode) && AdConfig.canShowGDTAd()) {
                        return VIDEO_AD_SOURCE_GDT;
                    }
                    if (VIDEO_AD_SOURCE_KY.equals(currentVideoSourceCode) && AdConfig.canShowCSJAd()) {
                        return VIDEO_AD_SOURCE_TT;
                    }
                    return VIDEO_AD_SOURCE_DEFAULT;
                } else {
                    return VIDEO_AD_SOURCE_DEFAULT;
                }
            }
            int checkCount = 0;
            int nextIndexOf = 0;
            int currentIndex = videoAdOrderList.indexOf(currentVideoSourceCode);
            if (currentIndex != -1) {
                nextIndexOf = (currentIndex + 1) % videoAdOrderList.size();
            }
            int size = videoAdOrderList.size();
            while (checkCount < size && loadCount < size) {
                String videoAdCode = videoAdOrderList.get(nextIndexOf % videoAdOrderList.size());
                if ((VIDEO_AD_SOURCE_GDT.equals(videoAdCode) && AdConfig.canShowGDTAd())
                        || (VIDEO_AD_SOURCE_TT.equals(videoAdCode) && AdConfig.canShowCSJAd())
                ) {
                    return videoAdCode;
                } else {
                    nextIndexOf++;
                    checkCount++;
                }
            }
        }
        return VIDEO_AD_SOURCE_DEFAULT;
    }

    /**
     * @param source 设置开屏广告来源
     */
    public static void setVideoAdSource(int source) {
        SPUtils.getInstance(CONFIG_NAME).put(VIDEO_SOURCE, source);
    }

    /**
     * 获取上次开屏的序号
     *
     * @return
     */
    public static String getLastVideoAdCode() {
        int currentDayInt = DateUtil.getDayInt(new Date());
        int lastShowDayInt = SPUtils.getInstance(CONFIG_NAME).getInt(LAST_VIDEO_SHOW_DAY, 0);
        if (lastShowDayInt == currentDayInt) {
            return SPUtils.getInstance(CONFIG_NAME).getString(LAST_VIDEO_AD_CODE, "");
        } else {
            return "";
        }
    }

    /**
     * 保存上次开屏的序号
     *
     * @param
     */
    public static void saveLastVideoAdCode(String code) {
        SPUtils.getInstance(CONFIG_NAME).put(LAST_VIDEO_AD_CODE, code);
        int showDay = DateUtil.getDayInt(new Date());
        SPUtils.getInstance(CONFIG_NAME).put(LAST_VIDEO_SHOW_DAY, showDay);
    }

    /**
     * 保存每次新协议已经读取的code,并且对应老的code
     * @param adInfo
     */
    public static void saveLastVideoAdCode(AdInfo adInfo) {
        switch (adInfo.getType()) {
            case AdInfo.AD_TYPE_GUANG_DIAN_TONG:
                SPUtils.getInstance(CONFIG_NAME).put(LAST_VIDEO_AD_CODE, VIDEO_AD_SOURCE_GDT);
                break;
            case AdInfo.AD_TYPE_CHUAN_SHAN_JIA:
                SPUtils.getInstance(CONFIG_NAME).put(LAST_VIDEO_AD_CODE, VIDEO_AD_SOURCE_TT);
                break;
        }
        int showDay = DateUtil.getDayInt(new Date());
        SPUtils.getInstance(CONFIG_NAME).put(LAST_VIDEO_SHOW_DAY, showDay);
    }

    /**
     * @param splashOrder 保存开屏广告顺序
     */
    public static void saveVideoAdOrder(String splashOrder) {
        SPUtils.getInstance(CONFIG_NAME).put(VIDEO_AD_ORDER, splashOrder);
    }

    /**
     * @return 获取开屏广告顺序
     */
    private static String getVideoAdOrder() {
        return SPUtils.getInstance(CONFIG_NAME).getString(VIDEO_AD_ORDER, DEFAULT_VIDEO_AD_ORDER);
    }
}
